<?php
/**
 * variable for this view:
 * 1)attributeView
 * 2)mediaIndex
 * 3)owner
 * 4)model
 * 5)model attribute fields
 */
/**
 * below code will populate attribute for product
 */
foreach ($attributeView as $id => $attribute) {
    $nameItem = $attribute->getItem('name');
    $typeItem = $attribute->getItem('type');
    $name = '';
    $type = '';
    $attributeValue = '';
    if ($nameItem instanceof BTreeItem) {
        $name = $nameItem->getValue();
        $attributeField = 'media_' . $name . '_' . $mediaIndex;
        $model->addField($attributeField);
        //get value of attribute in owner

        if ($owner instanceof BTreeList) {
            $attributeItem = $owner->getItem($name);
            if ($attributeItem instanceof BTreeItem) {
                $attributeValue = $attributeItem->getValue();
            }
        } else if ($owner instanceof BEntity) {
            $attrValueTable = new BAAttributeSheet($owner);
            $list = $attrValueTable->getList(array('itemData' => array('name' => $name)));
            if ($list instanceof BTreeList) {
                $attributeValue = $list->getItem('value')->getValue();
            }
        }

        if (isset($attributeValue) && $attributeValue != NULL) {
            $model->$attributeField = $attributeValue;
        } else
            $model->$attributeField = '';
    }
    if ($typeItem instanceof BTreeItem) {
//            $type = $nameItem->getValue();
//            $attributeField = 'media_' . $type . '_product_info';
        $type = $typeItem->getValue();
        if ($type == 'image') {
            $attributeField = 'image_media_' . $name . '_' . $mediaIndex;
            $model->addField($attributeField);
        }

        if ($owner instanceof BTreeList) {
            $attributeItem = $owner->getItem($name);
            if ($attributeItem instanceof BTreeItem) {
                $attributeValue = $attributeItem->getValue();
            }
        } else if ($owner instanceof BEntity) {
            $attrValueTable = new BAAttributeSheet($owner);
            $list = $attrValueTable->getList(array('itemData' => array('name' => $name)));
            if ($list instanceof BTreeList) {
                $attributeValue = $list->getItem('value')->getValue();
            }
        }

        if (isset($attributeValue) && $attributeValue != NULL) {
            $model->$attributeField = $attributeValue;
        } else
            $model->$attributeField = '';
    }
}
?>
<?php
$attributeControl = new BUIAttribute();
?>
<div  class="well-small row-fluid">
    <div class="input-prepend span3">
        <span class="add-on">属性名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeNameField, array('placeholder' => 'attribute' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">别名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeAliasField, array('placeholder' => 'alias' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">类型</span>
        <?php
        $typeList = $attributeControl->getDataTypeList();
        echo CHtml::activeDropDownList($model, $attributeTypeField, $typeList, array('required' => true, 'class' => 'span8'));
        ?>
    </div>
    <div class="input-append span2">
        <?php
//        echo $control->getAjaxLink(array('name' => '设置数据', 'id' => 'setDataTypeType'), array(
//            'htmlOptions' => array('class' => 'btn'),
//            'productId' => $productId,
//            'attributeId' => 0
//        ));
        ?>
        <?php
        echo $control->getAjaxLink(array('name' => '添加属性', 'id' => 'addAttribute'), array(
            'ownerClass' => $ownerClass,
            'ownerType' => $ownerType,
            'ownerId' => $ownerId,
            'attributeTable' => $attributeTable,
            'mediaIndex' => $mediaIndex,
            'htmlOptions' => array('class' => 'btn'),
            'attributeId' => 0
        ));
        ?>
    </div>

</div>
<!--<div class="row-fluid">
    <div class="span2 offset9">
<?php
//        echo $control->getAjaxLink(array('name' => '刷新属性', 'id' => 'product_add_info_refresh'), array(
//            'productId' => $productId
//        ));
?>
    </div>
</div>-->
<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span6">
        <col class="span2">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo '属性'; ?></th>
            <th><?php echo '内容'; ?></th>
            <th><?php echo '类型'; ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($attributeView as $attribute) {
            ?>
            <tr>
                <?php
                $nameItem = $attribute->getItem('name');
                $typeItem = $attribute->getItem('type');
                $aliasItem = $attribute->getItem('alias');
                $name = '';
                $type = '';
                $alias = '';
                $attributeValue = '';
                if ($nameItem instanceof BTreeItem) {
                    $name = $nameItem->getValue();
                    $attributeField = 'media_' . $name . '_' . $mediaIndex;
                    if ($owner instanceof BTreeList) {
                        $attributeItem = $owner->getItem($name);
                    } else {
                        $attrValueTable = new BAAttributeSheet($owner);
                        $list = $attrValueTable->getList(array('itemData' => array('name' => $name)));
                        if ($list instanceof BTreeList) {
                            $attributeItem = $list->getItem('value');
                        }
                    }
                    if (isset($attributeItem) && $attributeItem instanceof BTreeItem) {
                        $attributeValue = $attributeItem->getValue();
                        $model->$attributeField = $attributeValue;
                    } else
                        $model->$attributeField = '';
                }
                if ($typeItem instanceof BTreeItem) {
                    $type = $typeItem->getValue();
                    //get typeData from define in attribute
                    if ($type == 'image') {
                        $attributeField = 'image_media_' . $name . '_' . $mediaIndex;
                        $attributeItem = $owner->getItem($name);
                        if ($attributeItem instanceof BTreeItem) {
                            $attributeValue = $attributeItem->getValue();
                            $model->$attributeField = $attributeValue;
                        } else
                            $model->$attributeField = '';
                    }

                    $typeData = $attributeControl->getModelDataByDataType($typeItem);
                }
                if ($aliasItem instanceof BTreeItem) {
                    $alias = $aliasItem->getValue();
                }
                if ($alias == '')
                    $alias = $name;
                ?>
                <td> <?php echo $alias; ?>  </td>   
                <td> <?php
                    $modelData = array();
                    $modelData['model'] = $model;
                    $modelData['field'] = $attributeField;
                    $modelData['data'] = $typeData;
                    if (isset($form))
                        $modelData['form'] = $form;
                    echo $attributeControl->showActiveFieldByDataType($type, $modelData);
                    ?>  </td>
                <td> <?php echo $type; ?>  </td>
            </tr>
            <?php
        }
        ?>
    </tbody>
</table>
